/**
 * Joypad.java
 *
 * @author Somanath
 * @version 1.1 (last updated 16/6/07)
 *
 * class Dependencies: None
 */

 package gamepack;


 public class Joypad {

        public boolean[] key; // A set of keys of the joypad

        // The meaning of the keys, ie mapping of boolean[] key
        // to the joypad keys. ie The order in which the keys of joypad are laid.
        public int[] options;

        // Minimum number of supported buttons on keyboard.
        public static int MIN_BUTTONS = 12;

        // To check if left key if pressed
        // return key[option[Joypad.Left]];

        // The standard options - Set of keys defined on Joypad.
        public static int Left = Direction.WEST;
        public static int Right = Direction.EAST;
        public static int Up = Direction.NORTH;
        public static int Down = Direction.SOUTH;
        public static int ButtonA = 4;   // Generally used for Action
        public static int ButtonB = 5;   // Generally used for Jump
        public static int ButtonX = 6;
        public static int ButtonY = 7;
        public static int ButtonL1 = 8;
        public static int ButtonR1 = 9;
        public static int ButtonL2 = 10;
        public static int ButtonR2 = 11;

        // The default order in which the keys are laid.
        public static int[] defaultOption = {
               Left, Right, Up, Down,
               ButtonA, ButtonB, ButtonX, ButtonY,
               ButtonL1, ButtonR1, ButtonL2, ButtonR2
        };

        public String toString() {
               String s = "[";
               for(int i=0;i<MIN_BUTTONS;i++) {
                     int n = (key[options[i]] == true) ? 1 : 0;
                     s += " " + n;
               }
               s += "]";
               return s;

        }

        // For directions use -
        // Direction.LEFT, Direction.RIGHT, Direction.WEST, Direction.SOUTH

        // This array size must be atleast Joypad.MIN_BUTTONS or more.
        public Joypad(boolean[] arrObj) {
               key = arrObj;
               options = defaultOption;
        }

        public Joypad(boolean[] arrObj,int[] userDefinedOptions) {
               key = arrObj;
               options = userDefinedOptions;
        }

        public void clear() {
               for(int i=0;i<MIN_BUTTONS;i++) {
                   key[options[i]] = false;
               }
        }
        public void clearButtons() {
               for(int i=4;i<12;i++) {
                   key[options[i]] = false;
               }
        }

        public void clearDirection() {
               for(int i=0;i<4;i++) {
                   key[options[i]] = false;
               }
        }

        public void setDirection(int button,boolean value) {
               clearDirection();
               key[options[button]] = value;
        }


        public void set(int button,boolean value) {
               key[options[button]] = value;
        }

        public boolean get(int button) {
               return key[options[button]];
        }

 }